﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Hardcodet.Wpf.TaskbarNotification;
using FBApp.Classes;
using System.Threading;
using Facebook;
using System.Collections.ObjectModel;
using Microsoft.Expression.Interactivity.Core;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;

namespace FBApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
		private FacebookClient fbc;
		private Notifier notifier;
        private MainWindow instance;
        private ImageSource profilePic;

        public MainWindow()
        {
            InitializeComponent();
            //disableComponents();
            //loadingIndicator.start();
            //ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Settings", true);
            instance = this;
            notifier = new Notifier();
            iSettings.webBrowser1.Navigated += new NavigatedEventHandler(webBrowser1_Navigated);            
            fbc = new FacebookClient(notifier,iSettings.getWebBrowser());
            iNotifications.setFacebookClient(fbc);
            iNotifications.setIndicator(loadingIndicator);

            iNewsFeed.setFacebookClient(fbc);
            iNewsFeed.setIndicator(loadingIndicator);

            iWallFeed.setFacebookClient(fbc);
            iWallFeed.setIndicator(loadingIndicator);

            iMessages.setFacebookClient(fbc);
            iMessages.setIndicator(loadingIndicator);

            fbc.setNewsFeed(iNewsFeed);
            fbc.setNotificationFeed(iNotifications);
            fbc.setWallFeed(iWallFeed);
            fbc.setMessageFeed(iMessages);

            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                notifier.displayError("Error Connecting to Facebook Server", "Check network connection.");
            }            
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Settings", true);
        }

        public void disableComponents()
        {
            iNotifications.IsEnabled = false;
            iNewsFeed.IsEnabled = false;
            //iSettings.IsEnabled = false;
        }

        public void enableComponents()
        {
            iNotifications.IsEnabled = true;
            iNewsFeed.IsEnabled = true;
            iSettings.IsEnabled = true;
        }


        void webBrowser1_Navigated(object sender, NavigationEventArgs e)
        {            
            if (iSettings.webBrowser1.Source.ToString().Contains("token"))
            {                
                fbc.Authenticate(iSettings.webBrowser1.Source.ToString());
                if (fbc.IsAuthenticated())
                {                                        
                    //loadingIndicator.stop();
                    ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Notifications", true);

                    BackgroundWorker threadProfilePicture = new BackgroundWorker();                    

                    threadProfilePicture.DoWork += delegate
                    {                        
                        profilePic = fbc.getProfilePicture();
                    };                    
                    threadProfilePicture.RunWorkerCompleted += new RunWorkerCompletedEventHandler(threadProfilePicture_RunWorkerCompleted);
                    threadProfilePicture.RunWorkerAsync();                    
                    
                    enableComponents();
                    fbc.enableNotifications();                    
                }
            }
        }

        void threadProfilePicture_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {            
            if (profilePic != null)
            {                
                imgProfilePic.Source = profilePic;
                bProfilePic.IsEnabled = true;
            }            
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                TaskbarIcon tb = (TaskbarIcon)this.FindResource("tb");
                tb.Dispose();
                fbc.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading resource Taskbar Icon: " + ex.Message);
            }
        }

        private void btnShare_Click(object sender, RoutedEventArgs e)
        {
            if (!txtStatus.Text.Equals("") && !txtStatus.Equals("What's on your mind?"))
            {
                fbc.postStatus(txtStatus.Text);
				txtStatus.Text = "What's on your mind?";
                fbc.updateNotifications();
            }            
        }

        private void txtStatus_GotMouseCapture(object sender, MouseEventArgs e)
        {
            txtStatus.Text = "";                        
        }

        private void bNotifications_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {            
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Notifications", true);            
        }

        private void bMessages_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {            
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Messages", true);
        }

        private void bSettings_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "Settings", true);
        }

        private void bWallFeed_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "WallFeed", true);
        }

        private void bNewsFeed_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "NewsFeed", true);
        }

        private void imgProfilePic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (fbc.IsAuthenticated())
            {
                System.Diagnostics.Process.Start("http://www.facebook.com");                               
            }
        }         
    }
}
